package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbTypeTemplateMapper;
import com.dongdongshop.model.TbTypeTemplate;
import com.dongdongshop.model.TbTypeTemplateExample;
import com.dongdongshop.service.TypeService;
import com.dongdongshop.vo.BrandVo;
import com.dongdongshop.vo.TypeTemplateVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TbTypeTemplateMapper tbTypeTemplateMapper;

    @Override
    public List<TypeTemplateVo> typeList(TypeTemplateVo type) {
        TbTypeTemplateExample example = new TbTypeTemplateExample();
        TbTypeTemplateExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(type.getName())){
            criteria.andNameLike("%"+type.getName()+"%");
        }

        List<TbTypeTemplate> typeTemplates = tbTypeTemplateMapper.selectByExample(example);

        return typeTemplates.stream().map(typeTemplate -> {
            TypeTemplateVo vo = new TypeTemplateVo();
            BeanUtils.copyProperties(typeTemplate, vo);
            return vo;
        }).collect(Collectors.toList());
    }


    @Override
    public void addType(TbTypeTemplate tbTypeTemplate) {
        tbTypeTemplateMapper.insertSelective(tbTypeTemplate);
    }


    //删除
    @Override
    public void deletes(Long[] ids) {
        TbTypeTemplateExample example = new TbTypeTemplateExample();
        TbTypeTemplateExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(Arrays.asList(ids));
        tbTypeTemplateMapper.deleteByExample(example);
    }



    //回显
    @Override
    public TypeTemplateVo toupdate(Long id) {
        TbTypeTemplate tbTypeTemplate = tbTypeTemplateMapper.selectByPrimaryKey(id);
        TypeTemplateVo vo = new TypeTemplateVo();
        BeanUtils.copyProperties(tbTypeTemplate,vo);
        return vo;
    }
}
